package com.halflife.comeElectricity.basic.goodsType;

import com.halflife.comeElectricity.member.merchant.Merchant;
import com.halflife.shiro.utils.ShiroUtil;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by zhaoyf on 2018/2/4.
 */
@RestController
@RequestMapping("GoodsType")
public class GoodsTypeController {
    @Resource
    private GoodsTypeRepository goodsTypeRepository;


    @GetMapping("/{id}")
    public GoodsType getGoodsType(@PathVariable Integer id) {
        return goodsTypeRepository.selectByPrimaryKey(id);
    }

    @GetMapping("")
    public List<GoodsType> getGoodsType(HttpServletRequest request) {
        Merchant merchant = ShiroUtil.getMerchant(request);
        return goodsTypeRepository.selectAll();
    }

    @PostMapping("")
    public ResponseEntity addGoodsType(@RequestBody GoodsType merchant, HttpServletRequest request) {
        Integer id = goodsTypeRepository.insert(merchant);
        return new ResponseEntity(merchant, HttpStatus.OK);
    }

    @PutMapping("")
    public ResponseEntity updateGoodsType(@RequestBody GoodsType merchant, HttpServletRequest request) {
        Integer id = goodsTypeRepository.updateByPrimaryKey(merchant);
        return new ResponseEntity(merchant, HttpStatus.OK);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity deleteGoodsType(@PathVariable Integer id, HttpServletRequest request) {
        Integer key = goodsTypeRepository.deleteByPrimaryKey(id);
        return new ResponseEntity(key, HttpStatus.OK);
    }
}
